const fxaa_frag_multisampled = /*wgsl*/ `
override FXAA_REDUCE_MIN:f32=1.0/128.0;
override FXAA_REDUCE_MUL:f32=1.0/8.0;
override FXAA_SPAN_MAX:f32=8.0;  
fn combieSample(Texture:texture_multisampled_2d<f32>,uv:vec2<u32>)->vec4<f32>{
   var sampler1= textureLoad(Texture,uv,0);
   var sampler2= textureLoad(Texture,uv,1);
   var sampler3= textureLoad(Texture,uv,2);
   var sampler4= textureLoad(Texture,uv,3);
   var average=(sampler1+sampler2+sampler3+sampler4)/4;
   return average;
}
fn fxaa(Sampler:sampler,Texture:texture_multisampled_2d<f32>,input:Input)->vec4<f32> {
    var color:vec4<f32>;
       //  获取纹理尺寸
    let texSize =vec2<f32>(textureDimensions(Texture).xy);
    var rgbNW:vec3<f32> = combieSample(Texture,vec2<u32>(u32(input.rgbNW.x),u32(input.rgbNW.y))).xyz;
    var rgbNE:vec3<f32> = combieSample(Texture,vec2<u32>(u32(input.rgbNE.x),u32(input.rgbNE.y))).xyz;
    var rgbSW:vec3<f32> = combieSample(Texture,vec2<u32>(u32(input.rgbSW.x),u32(input.rgbSW.y))).xyz;
    var rgbSE:vec3<f32> = combieSample(Texture,vec2<u32>(u32(input.rgbSE.x),u32(input.rgbSE.y))).xyz;
    var texColor:vec4<f32> = combieSample(Texture,vec2<u32>(u32(input.rgbM.x),u32(input.rgbM.y)));
    var rgbM:vec3<f32>  = texColor.xyz;
    var luma:vec3<f32> = vec3<f32>(0.299, 0.587, 0.114);
    var lumaNW:f32 = dot(rgbNW, luma);
    var lumaNE:f32 = dot(rgbNE, luma);
    var lumaSW:f32 = dot(rgbSW, luma);
    var lumaSE:f32 = dot(rgbSE, luma);
    var lumaM:f32  = dot(rgbM,  luma);
    var lumaMin:f32 = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
    var lumaMax:f32 = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
    
    var dir:vec2<f32>=vec2<f32>(0.0,0.0);
    dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
    dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));
    var dirReduce:f32 = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL),FXAA_REDUCE_MIN);
    
    var rcpDirMin:f32 = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
    var inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
    dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
            max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
            dir * rcpDirMin)) * inverseVP;
      var uv1Offset=input.uv+ dir * (1.0 / 3.0 - 0.5)*texSize;
      var uv2Offset=input.uv+ dir * (2.0 / 3.0 - 0.5)*texSize;
    var rgbA:vec3<f32> = 0.5 * (
        combieSample(Texture,vec2<u32>(u32(uv1Offset.x),u32(uv1Offset.y))).xyz+
        combieSample(Texture,vec2<u32>(u32(uv2Offset.x),u32(uv2Offset.y))).xyz);
        var uv3Offset=input.uv+ dir * -0.5*texSize;
        var uv4Offset=input.uv+ dir * 0.5*texSize;
    var rgbB:vec3<f32> = rgbA * 0.5 + 0.25 * (
        combieSample(Texture,vec2<u32>(u32(uv3Offset.x),u32(uv3Offset.y))).xyz+
        combieSample(Texture,vec2<u32>(u32(uv4Offset.x),u32(uv4Offset.y))).xyz);

    var lumaB:f32 = dot(rgbB, luma);
    if ((lumaB < lumaMin) || (lumaB > lumaMax)){
        color = vec4(rgbA, texColor.a);
    }else{
        color = vec4(rgbB, texColor.a);
    }  
    return color;
}
struct Input {  
    @location(0) uv : vec2<f32>,
    @location(1) rgbNW:vec2<f32>,
    @location(2) rgbNE:vec2<f32>,
    @location(3) rgbSW:vec2<f32>,
    @location(4) rgbSE:vec2<f32>,
    @location(5) rgbM:vec2<f32>,
};
struct Output {
    @location(0) color:vec4<f32>,
};
@group(0) @binding(0) var<uniform> resolution : vec2<f32>;
@group(0) @binding(1) var Sampler: sampler;
// @group(0) @binding(2) var Texture: texture_2d<f32>;
@group(0) @binding(2) var Texture: texture_multisampled_2d<f32>;
@fragment
fn main(input:Input) -> Output {
    var output:Output;
    var color=fxaa(Sampler,Texture, input);
    output.color=color;
    return output;
}
`;
const fxaa_frag = /*wgsl*/ `
override FXAA_REDUCE_MIN:f32=1.0/128.0;
override FXAA_REDUCE_MUL:f32=1.0/8.0;
override FXAA_SPAN_MAX:f32=8.0;  
fn fxaa(Sampler:sampler,Texture:texture_2d<f32>,input:Input)->vec4<f32> {
    var color:vec4<f32>;
    var rgbNW:vec3<f32> = textureSample(Texture, Sampler,input.rgbNW).xyz;
    var rgbNE:vec3<f32> = textureSample(Texture, Sampler,input.rgbNE).xyz;
    var rgbSW:vec3<f32> = textureSample(Texture, Sampler,input.rgbSW).xyz;
    var rgbSE:vec3<f32> = textureSample(Texture, Sampler,input.rgbSE).xyz;
    var texColor:vec4<f32> = textureSample(Texture, Sampler,input.rgbM);
    var rgbM:vec3<f32>  = texColor.xyz;
    var luma:vec3<f32> = vec3<f32>(0.299, 0.587, 0.114);
    var lumaNW:f32 = dot(rgbNW, luma);
    var lumaNE:f32 = dot(rgbNE, luma);
    var lumaSW:f32 = dot(rgbSW, luma);
    var lumaSE:f32 = dot(rgbSE, luma);
    var lumaM:f32  = dot(rgbM,  luma);
    var lumaMin:f32 = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
    var lumaMax:f32 = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
    
    var dir:vec2<f32>=vec2<f32>(0.0,0.0);
    dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
    dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));
    var dirReduce:f32 = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL),FXAA_REDUCE_MIN);
    
    var rcpDirMin:f32 = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
    var inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
    dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
            max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
            dir * rcpDirMin)) * inverseVP;
    
    var rgbA:vec3<f32> = 0.5 * (
        textureSample(Texture, Sampler,input.uv+ dir * (1.0 / 3.0 - 0.5)).xyz+
        textureSample(Texture, Sampler,input.uv+ dir * (2.0 / 3.0 - 0.5)).xyz);
    var rgbB:vec3<f32> = rgbA * 0.5 + 0.25 * (
        textureSample(Texture, Sampler,input.uv+ dir * -0.5).xyz+
        textureSample(Texture, Sampler,input.uv+ dir * 0.5).xyz);

    var lumaB:f32 = dot(rgbB, luma);
    if ((lumaB < lumaMin) || (lumaB > lumaMax)){
        color = vec4(rgbA, texColor.a);
    }else{
        color = vec4(rgbB, texColor.a);
    }  
    return color;
}
struct Input {  
    @location(0) uv : vec2<f32>,
    @location(1) rgbNW:vec2<f32>,
    @location(2) rgbNE:vec2<f32>,
    @location(3) rgbSW:vec2<f32>,
    @location(4) rgbSE:vec2<f32>,
    @location(5) rgbM:vec2<f32>,
};
struct Output {
    @location(0) color:vec4<f32>,
};
@group(0) @binding(0) var<uniform> resolution : vec2<f32>;
@group(0) @binding(1) var Sampler: sampler;
@group(0) @binding(2) var Texture: texture_2d<f32>;
// @group(0) @binding(0) var Texture: texture_multisampled_2d<f32>;
@fragment
fn main(input:Input) -> Output {
    var output:Output;
    var color=fxaa(Sampler,Texture, input);
    output.color=color;
    return output;
}
`;
export { fxaa_frag, fxaa_frag_multisampled };
